package com.example.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yunsuan);

         final EditText one =  findViewById(R.id.one);
        final EditText two = findViewById(R.id.two);
        final EditText fuhao = findViewById(R.id.fuhao);
        final TextView text = findViewById(R.id.textView3);
        final EditText yuce = findViewById(R.id.yuce);
        final ImageView image = findViewById(R.id.image1);

        //计算按钮
        Button jisuan = findViewById(R.id.button);
        //清空
        Button qk = findViewById(R.id.button2);
//计算按钮
        jisuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (one.getText() != null && !one.getText().toString().isEmpty()
                        && two.getText().toString() != null && !two.getText().toString().isEmpty()
                        && fuhao.getText() != null && !fuhao.getText().toString().isEmpty()
                        && yuce.getText() != null && !yuce.getText().toString().isEmpty()) {


                    int first = Integer.parseInt(one.getText().toString());
                    int second = Integer.parseInt(two.getText().toString());
                    int i = Integer.parseInt(yuce.getText().toString());

                    String s = fuhao.getText().toString();//符号


                    if (s.equals("+")) {
                        text.setText("结果" + (first + second));
                        if (i == (first + second)) {
                            image.setImageResource(R.drawable.dui);
                        } else {
                            image.setImageResource(R.drawable.cuowu);
                        }
                    } else if (s.equals("-")) {
                        int a = first - second;
                        text.setText("结果" + a);
                        if (i == (first - second)) {
                            image.setImageResource(R.drawable.dui);
                        } else {
                            image.setImageResource(R.drawable.cuowu);
                        }
                    } else if (s.equals("*")) {
                        text.setText("结果" + first * second);
                        if (i == (first * second)) {
                            image.setImageResource(R.drawable.dui);
                        } else {
                            image.setImageResource(R.drawable.cuowu);
                        }
                    } else if (s.equals("/")) {

                        text.setText("结果" + first / second);
                        if (i == (first / second)) {
                            image.setImageResource(R.drawable.dui);
                        } else {
                            image.setImageResource(R.drawable.cuowu);
                        }
                    } else {
                        text.setText("运算符错误");
                    }
                } else {//输入有null
                    text.setText("输入不能为空");
                }
            }
        });

        qk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                one.setText("");
                two.setText("");
                fuhao.setText("");
                image.setImageResource(0);
            }
        });
    }
}
